package org.oslims.client.uicomponents.editbox;

import org.oslims.domain.Report;
import org.oslims.domain.Request;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;

public class EditReportsBox {

	final DialogBox editReportsBox = new DialogBox(false, true);
	final VerticalPanel editReportBoxWrapperPanel = new VerticalPanel();
	final FormPanel editReportsBoxPanel = new FormPanel();

	final CellTable<Report> reportTable = new CellTable<Report>();
	final FileUpload uploader = new FileUpload();
	final Button close = new Button("Close");
	final Hidden requestId = new Hidden();
	final Hidden labStaffId = new Hidden();
	private Request editedRequest;

	public EditReportsBox() {
		editReportsBoxPanel.setAction("/oslims/uploadService");
		editReportsBoxPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
		editReportsBoxPanel.setMethod(FormPanel.METHOD_POST);

		VerticalPanel vp = new VerticalPanel();
		editReportsBoxPanel.setWidget(vp);

		editReportBoxWrapperPanel.add(reportTable);

		uploader.setName("uploadWidget");

		vp.add(uploader);

		requestId.setName("requestId");
		vp.add(requestId);

		labStaffId.setName("labStaffId");
		vp.add(labStaffId);

		editReportBoxWrapperPanel.add(editReportsBoxPanel);

		// Add a 'submit' button.
		vp.add(new Button("Submit", new ClickHandler() {
			public void onClick(ClickEvent event) {
				editReportsBoxPanel.submit();
			}
		}));

		editReportsBoxPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {

			@Override
			public void onSubmitComplete(SubmitCompleteEvent arg0) {
				reportTable.refresh();
			}
		});

		vp.add(close);

		close.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				editReportsBox.hide();
			}
		});

		editReportsBox.add(editReportBoxWrapperPanel);
	}

	public void show() {
		editReportsBox.center();
	}

	public CellTable<Report> getReportTable() {
		return reportTable;
	}

	public FileUpload getUploader() {
		return uploader;
	}

	public Button getClose() {
		return close;
	}

	public Hidden getRequestId() {
		return requestId;
	}

	public Hidden getLabStaffId() {
		return labStaffId;
	}

	public void setEditedRequest(Request editedRequest) {
		this.editedRequest = editedRequest;
	}

	public Request getEditedRequest() {
		return editedRequest;
	}

}
